package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.VideoDAO;
import bist.believeinstep.domain.Video;
import bist.believeinstep.mapper.VideoMapper;

public class VideoTemplate implements VideoDAO, ConnectionDAO{
	private JdbcTemplate jdbcTemplate;
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(Video video){
		String query = "INSERT INTO bist_videos (code, name, introduction, descriptions, state, type, link, "
				+ "created_date, modified_date, created_by, modified_by) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
					video.getCode(),
					video.getName(),
					video.getIntroduction(),
					video.getDescriptions(),
					video.getState(),
					video.getType(),
					video.getLink(),
					video.getCreated(),
					video.getModified(),
					video.getCreatedBy(),
					video.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(Video video){
		String query = "UPDATE bist_video SET"
				+ "code = ?"
				+ "name =?"
				+ "introduction = ?"
				+ "descriptions = ?"
				+ "state = ?"
				+ "type = ?"
				+ "link = ?"
				+" created_date = ?"
				+" modified_date = ?"
				+" created_by = ?"
				+" modified_by = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				video.getCode(),
				video.getName(),
				video.getIntroduction(),
				video.getDescriptions(),
				video.getState(),
				video.getType(),
				video.getLink(),
				video.getCreated(),
				video.getModified(),
				video.getCreatedBy(),
				video.getModifiedBy()	
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_videos WHERE id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(String code){
		String query = "DELETE FROM bist_videos WHERE code = ?";
		try{
			jdbcTemplate.update(query, new Object[]{code});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public Video getVideo(Integer id){
		String query = "SELECT FROM bist_videos WHERE id = ?";
		try{
			Video video = jdbcTemplate.queryForObject(query, new Object[]{id}, new VideoMapper());
			return video;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public Video getVideo(String code){
		String query = "SELECT FROM bist_videos WHERE code = ?";
		try{
			Video video = jdbcTemplate.queryForObject(query, new Object[]{code}, new VideoMapper());
			return video;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Video> list(){
		String query = "SELECT * FROM bist_videos";
		try{
			List<Video> video = jdbcTemplate.query(query, new VideoMapper());
			return video;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Video>listVideo(String name){
		String query = "SELECT FROM bist_videos WHERE name = ?";
		try{
			List<Video>video = jdbcTemplate.query(query, new Object[]{name}, new VideoMapper());
			return video;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
